﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using Common.Service;
using OC.Common.Entity;

namespace OC
{
    public partial class UserGroupMember : System.Web.UI.Page
    {
        OC.Common.Interface.IDictionary _dictionaryService;

        public OC.Common.Interface.IDictionary DictionaryService
        {
            get
            {
                if (_dictionaryService == null)
                {
                    _dictionaryService = ServiceContainer.GetService<OC.Common.Interface.IDictionary>();
                }
                return _dictionaryService;
            }
            set { _dictionaryService = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitUserGroupView();
            }
        }

        private void InitUserGroupView()
        {
            IList userGroups = DictionaryService.GetListByDictionaryType("DictionaryLogic_GetListByDictionaryType", "USERGROUP");

            foreach (DictionaryEntity item in userGroups)
            {
                if (item.PARENT_DICTIONARY_ID.Trim() == "0")//先找到根节点
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = item.CN_NAME;
                    tn.Value = item.DICTIONARY_ID;
                    GetChildNodeByList(userGroups, item.DICTIONARY_ID, tn);
                    tvUserGroup.Nodes.Add(tn);
                    break;
                }
            }
            tvUserGroup.ExpandDepth = 1;
        }

        /// <summary>
        /// 根据ID在查询到的组织数据中轮询查找它的子节点
        /// </summary>
        /// <param name="structure"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        private TreeNode GetChildNodeByList(IList structure, string id, TreeNode ptn)
        {
            foreach (DictionaryEntity item in structure)
            {
                if (id == item.PARENT_DICTIONARY_ID)
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = item.CN_NAME;
                    tn.Value = item.DICTIONARY_ID;
                    ptn.ChildNodes.Add(tn);
                    foreach (DictionaryEntity citem in structure)
                    {
                        if (item.DICTIONARY_ID == citem.PARENT_DICTIONARY_ID)
                        {
                            TreeNode ctn = new TreeNode();
                            ctn.Text = citem.CN_NAME;
                            ctn.Value = citem.DICTIONARY_ID;
                            tn.ChildNodes.Add(ctn);
                        }
                    }
                }
            }
            return ptn;
        }

        protected void tvUserGroup_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
        {
            TreeNode tn = e.Node;
            if (tn.ChildNodes.Count > 0)
            {
                foreach (TreeNode item in tn.ChildNodes)
                {
                    if (item.ChildNodes.Count <= 0)
                    {
                        IList dicEntitys = DictionaryService.GetList(item.Value);
                        foreach (DictionaryEntity dics in dicEntitys)
                        {
                            TreeNode ctn = new TreeNode();
                            ctn.Text = dics.CN_NAME;
                            ctn.Value = dics.DICTIONARY_ID;
                            item.ChildNodes.Add(ctn);
                        }
                    }
                }
            }
        }

        protected void tvUserGroup_SelectedNodeChanged(object sender, EventArgs e)
        {
            TreeNode tn = tvUserGroup.SelectedNode;
            DictionaryEntity dicEntity = DictionaryService.Get(tn.Value);
            if (dicEntity != null)
            {
                txtGroupName.Text = tn.Text;
                txtDescription.Text = dicEntity.EXT15;
                IList dicUserList = DictionaryService.GetDictionaryRelationByRelation_Type("USER", tn.Value);//在字典关系表中查询User关系
                List<string> userIDs = new List<string>();
                foreach (DictionaryRelationEntity item in dicUserList)
                {
                    userIDs.Add(item.DESTINATION_DICTIONARY_ID);
                }
                IList userList = DictionaryService.GetUserByUserIDs(userIDs);
                List<CustomEntity> roleGroupView = new List<CustomEntity>();
                foreach (UserEntity user in userList)
                {
                    CustomEntity roleUser = new CustomEntity();
                    roleUser.Ext01 = "人员";
                    roleUser.Ext02 = user.CN_NAME;
                    roleUser.Ext03 = user.USER_CODE;
                    if (!string.IsNullOrEmpty(user.SECTION_ID))
                    {
                        DictionaryEntity dictionaryEntity = DictionaryService.Get(user.SECTION_ID);
                        roleUser.Ext04 = dictionaryEntity != null ? dictionaryEntity.CN_NAME : string.Empty;
                    }
                    else if (!string.IsNullOrEmpty(user.DEPARTMENT_ID))
                    {
                        DictionaryEntity dictionaryEntity = DictionaryService.Get(user.DEPARTMENT_ID);
                        roleUser.Ext04 = dictionaryEntity != null ? dictionaryEntity.CN_NAME : string.Empty;
                    }
                    else if (!string.IsNullOrEmpty(user.COMPANY_ID))
                    {
                        DictionaryEntity dictionaryEntity = DictionaryService.Get(user.COMPANY_ID);
                        roleUser.Ext04 = dictionaryEntity != null ? dictionaryEntity.CN_NAME : string.Empty;
                    }
                    roleGroupView.Add(roleUser);
                }
                List<string> dicIDs = new List<string>();
                IList dicOrganizationList = DictionaryService.GetDictionaryRelationByRelation_Type("ORGANIZATION", tn.Value);//在字典关系表中查询组织关系
                foreach (DictionaryRelationEntity item in dicOrganizationList)
                {
                    dicIDs.Add(item.DESTINATION_DICTIONARY_ID);
                }
                IList dicList = DictionaryService.GetDictionaryByIDs(dicIDs);
                
                foreach (DictionaryEntity item in dicList)
                {
                    CustomEntity roleOrganization = new CustomEntity();
                    roleOrganization.Ext01 = "部门";
                    roleOrganization.Ext02 = item.CN_NAME;
                    roleOrganization.Ext03 = item.DICTIONARY_CODE;
                    if (item.PARENT_DICTIONARY_ID.Trim() != "0")
                    {
                        DictionaryEntity dictionaryEntity = DictionaryService.Get(item.PARENT_DICTIONARY_ID);
                        roleOrganization.Ext04 = dictionaryEntity != null ? dictionaryEntity.CN_NAME : string.Empty;
                    }
                    else
                    {
                        roleOrganization.Ext04 = "无";
                    }
                    roleGroupView.Add(roleOrganization);
                }

                rptUserList.DataSource = roleGroupView;
                rptUserList.DataBind();
            }
        }
    }
}